<?php

namespace app\modules\api\models;

use Yii;
/**
* @OA\Schema(
*      schema="Field",
*      required={"module","name","max_length","created_at","updated_at"},
*     @OA\Property(
*        property="id",
*        description="主键",
*        type="integer",
*        format="int64",
*    ),
*     @OA\Property(
*        property="module",
*        description="所在模块",
*        type="integer",
*        format="int64",
*    ),
*     @OA\Property(
*        property="type",
*        description="信息类型",
*        type="integer",
*        format="int64",
*        default="1",
*    ),
*     @OA\Property(
*        property="form_type",
*        description="字段类型",
*        type="integer",
*        format="int64",
*        default="1",
*    ),
*     @OA\Property(
*        property="name",
*        description="名称",
*        type="string",
*        maxLength=32,
*    ),
*     @OA\Property(
*        property="max_length",
*        description="最大长度",
*        type="integer",
*        format="int64",
*    ),
*     @OA\Property(
*        property="default_value",
*        description="默认值",
*        type="string",
*        maxLength=32,
*        default="",
*    ),
*     @OA\Property(
*        property="value",
*        description="值",
*        type="string",
*        maxLength=128,
*        default="",
*    ),
*     @OA\Property(
*        property="tips",
*        description="提示语",
*        type="string",
*        maxLength=32,
*        default="",
*    ),
*     @OA\Property(
*        property="is_validate",
*        description="是否验证",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="is_unique",
*        description="是否唯一",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="is_check",
*        description="是否去重",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="is_allow_empty",
*        description="是否允许为空",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="in_list_show",
*        description="列表页显示",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="in_add_show",
*        description="添加页显示",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
*     @OA\Property(
*        property="status",
*        description="状态",
*        type="integer",
*        format="int64",
*        default="1",
*    ),
*     @OA\Property(
*        property="auth",
*        description="权限",
*        type="integer",
*        format="int64",
*        default="1",
*    ),
*     @OA\Property(
*        property="sort",
*        description="排序",
*        type="integer",
*        format="int64",
*        default="0",
*    ),
* )
*/

/**
 * This is the model class for table "{{%field}}".
 *
 * @property int $id 主键
 * @property int $module 所在模块
 * @property int $type 信息类型
 * @property int $form_type 字段类型
 * @property string $name 名称
 * @property int $max_length 最大长度
 * @property string $default_value 默认值
 * @property string $value 值
 * @property string $tips 提示语
 * @property int $is_validate 是否验证
 * @property int $is_unique 是否唯一
 * @property int $is_check 是否去重
 * @property int $is_allow_empty 是否允许为空
 * @property int $in_list_show 列表页显示
 * @property int $in_add_show 添加页显示
 * @property int $status 状态
 * @property int $auth 权限
 * @property int $sort 排序
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 */
class Field extends \yii\db\ActiveRecord
{
    public function behaviors()
    {
        return [
            [
                'class' => \yii2tech\ar\softdelete\SoftDeleteBehavior::className(),
                'softDeleteAttributeValues' => [
                    'deleted_at' =>  time(),
                ],
                'restoreAttributeValues' => [
                    'deleted_at' => 0
                ]
//                'replaceRegularDelete' => true
            ],
            [
                'class' => \yii\behaviors\TimestampBehavior::className(),
                'createdAtAttribute' => 'created_at',
                'updatedAtAttribute' => 'updated_at',
                'value' =>  time(),
            ],
            [
                'class' => \yii\behaviors\BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',
            ],
        ];
    }
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%field}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['module', 'name', 'max_length', 'created_at', 'updated_at'], 'required'],
            [['module', 'type', 'form_type', 'max_length', 'is_validate', 'is_unique', 'is_check', 'is_allow_empty', 'in_list_show', 'in_add_show', 'status', 'auth', 'sort', 'created_at', 'updated_at'], 'integer'],
            [['name', 'default_value', 'tips'], 'string', 'max' => 32],
            [['value'], 'string', 'max' => 128],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => '主键',
            'module' => '所在模块',
            'type' => '信息类型',
            'form_type' => '字段类型',
            'name' => '名称',
            'max_length' => '最大长度',
            'default_value' => '默认值',
            'value' => '值',
            'tips' => '提示语',
            'is_validate' => '是否验证',
            'is_unique' => '是否唯一',
            'is_check' => '是否去重',
            'is_allow_empty' => '是否允许为空',
            'in_list_show' => '列表页显示',
            'in_add_show' => '添加页显示',
            'status' => '状态',
            'auth' => '权限',
            'sort' => '排序',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }


    public static function find()
    {
    $query = parent::find();

    $query->attachBehavior('softDelete', \yii2tech\ar\softdelete\SoftDeleteQueryBehavior::className());

    return $query->notDeleted();
    }

    public function fields()
    {
        $fields = parent::fields();
        $customFields = [
            'created_at' => function ($model) {
                return \Yii::$app->formatter->asDatetime($model->created_at,'php:c');
            },
            'updated_at' => function ($model) {
                return \Yii::$app->formatter->asDatetime($model->updated_at,'php:c');
            },
        ];
        unset($fields['deleted_at']);

        return \yii\helpers\ArrayHelper::merge($fields, $customFields);
    }

    public function extraFields()
    {
        return [
            'creator',
            'updater'
        ];
    }

    public function getCreator()
    {
        return $this->hasOne(User::className(), ['id' => 'created_by']);
    }

    public function getUpdater()
    {
        return $this->hasOne(User::className(), ['id' => 'updated_by']);
    }

}
